﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class FeedbackController : BaseController
    {
        private feedbackCore core = new feedbackCore();
        [HttpPost]
        public ActionResult Save(feedbackEntity feedback)
        {
            var referrer = Request.UrlReferrer;
            if (referrer==null||referrer.Host != Request.Url.Host)
            {
                return Redirect("/error/index");
            }
            if (string.IsNullOrEmpty(feedback.tel)&&string.IsNullOrEmpty(feedback.email))
            {
                if (string.IsNullOrEmpty(feedback.content))
                {
                    return Redirect("/error/index?err=内容不够完善");
                }
                else
                {
                    return Redirect("/error/index?err=内容不够完！");
                }
            }
            sysinfoCore sysinfo=new sysinfoCore();
            var isyanzheng = false;
            var error = string.Empty;
            if (sysinfo.GetOne().code)
            {
                var code = Request.Form["code"].ToLower();
                if (Session["code"] == null || code != Session["code"].ToString().ToLower())
                {
                    Utils.ResetCode();
                    error= "验证码错误";
                    isyanzheng = false;
                }
                else
                {
                    isyanzheng = true;
                }
            }
            else
            {
                isyanzheng = true;

            }

            if (!string.IsNullOrEmpty(feedback.tel))
            {

                if (feedback.tel.Length < 10)
                {

                    error = "电话格式不正确";
                    isyanzheng = false;
                }
                else
                {
                    isyanzheng = true;
                }
            }

            if (!string.IsNullOrEmpty(feedback.email))
            {
                if(Utils.IsEmail(feedback.email))
                    isyanzheng = true;
                else
                {
                    error = "邮箱格式不正确";
                    isyanzheng = false;
                }
            }
            if (isyanzheng)
            {
                feedback.addtime = DateTime.Now;
                feedback.ip = Utils.GetAddressIP();
            var model = core.Save(feedback);
            return Content(template("green",referrer.ToString(),"留言成功"), "text/html;charset=utf-8");

            }
            else
            {
                return Content(template("#000", referrer.ToString(), error,error), "text/html;charset=utf-8");
            }
        }

        private string template(string color, string url, string msg,string title= "提交成功！")
        {
            return $@"<!DOCTYPE HTML>
<html lang=""en-US"">
<head>
	<meta charset=""UTF-8"">
	<title>{title}</title>
</head>
<body>
	<h1 style=""text-align:center;margin:200px auto 0;color:{color}"">{msg}，页面将于3秒后跳转，点击<a href=""{url}"">此处立即跳转</a></h1>
	<script type=""text/javascript"">
	setTimeout(function(){{
		window.location.href=""{url}""
	}},3000);
	</script>
</body>
</html>";
        }
    }
}
